﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using XNAGuiLib.Core.Data;
using Microsoft.Xna.Framework.Graphics;

namespace XNAGuiLib.Core
{
    public class ContentComponent : Component
    {
        public bool Visible
        {
            get { return this._visible; }
            set { this._visible = value; }
        }

        public Rectangle Rectangle
        {
            get
            {
                if (_contTexture != null)
                    return _contTexture.Rectangle;
                else return new Rectangle();
            }
            set
            {
                if (_contTexture != null)
                    _contTexture.Rectangle = value;
            }
        }

        // the content Texture for this ContentComponent
        #region Public Field - ContTexture
        private XNAGuiLib.Core.Data.Texture _contTexture = null;
        /// <summary>
        /// gets or sets the ContentComponent's Texture-object
        /// </summary>
        public XNAGuiLib.Core.Data.Texture ContTexture
        {
            get
            {
                return _contTexture;
            }
            set
            {
                _contTexture = value;
            }
        }
        #endregion

        // the content Text for this ContentComponent
        #region Public Field - ContText
        private Text _contText = null;
        /// <summary>
        /// gets or sets the ContentComponent's Text-object
        /// </summary>
        public Text ContText
        {
            get
            {
                return _contText;
            }
            set
            {
                _contText = value;
            }
        }
        #endregion

        // Creates a new ContentComponent with a 'Texture' object as drawn content
        #region Constructor - ContentComponent(Texture2D textureContent)
        /// <summary>
        /// Creates a new ContentComponent with a 'Texture' object as drawn content
        /// </summary>
        /// <param name="textureContent">the Texture2D object used to create the 'Texture' object</param>
        public ContentComponent(Texture2D textureContent)
            : base()
        {
            this._contTexture = new XNAGuiLib.Core.Data.Texture(textureContent);
        }
        #endregion 

        // Creates a new ContentComponent with a 'Texture' object as drawn content
        #region Constructor - ContentComponent(Texture2D textureContent, Vector2 relativePosition)
        /// <summary>
        /// Creates a new ContentComponent with a 'Texture' object as drawn content
        /// </summary>
        /// <param name="textureContent">the Texture2D object used to create the 'Texture' object</param>
        /// <param name="relativePosition">the Vector2 on which the Texture2D is drawn on the screen (relative to the ContentComponents TotalPosition)</param>
        public ContentComponent(Texture2D textureContent, Vector2 relativePosition)
            : base()
        {
            this._contTexture = new XNAGuiLib.Core.Data.Texture(textureContent, relativePosition);
        }
        #endregion

        // Creates a new ContentComponent with a 'Texture' object as drawn content
        #region Constructor - ContentComponent(Texture2D textureContent, Rectangle rectangle)
        /// <summary>
        /// Creates a new ContentComponent with a 'Texture' object as drawn content
        /// </summary>
        /// <param name="textureContent">the Texture2D object used to create the 'Texture' object</param>
        /// <param name="rectangle">the Rectangle on which the Texture2D is drawn on the screen (relative to the ContentComponents TotalPosition)</param>
        public ContentComponent(Texture2D textureContent, Rectangle rectangle)
            : base()
        {
            this._contTexture = new XNAGuiLib.Core.Data.Texture(textureContent, rectangle);
        }
        #endregion

        // Creates a new ContentComponent with a 'Texture' object as drawn content
        #region Constructor - ContentComponent(Texture2D textureContent, Rectangle rectangle, Color modulationColor)
        /// <summary>
        /// Creates a new ContentComponent with a 'Texture' object as drawn content
        /// </summary>
        /// <param name="textureContent">the Texture2D object used to create the 'Texture' object</param>
        /// <param name="rectangle">the Rectangle on which the Texture2D is drawn on the screen (relative to the ContentComponents TotalPosition)</param>
        /// <param name="modulationColor">the Color which will be used to modulate the Texture2D object while drawing</param>
        public ContentComponent(Texture2D textureContent, Rectangle rectangle, Color modulationColor)
            : base()
        {
            this._contTexture = new XNAGuiLib.Core.Data.Texture(textureContent, rectangle, modulationColor);
        }
        #endregion

        // Creates a new ContentComponent with a 'Text' object as drawn content
        #region Constructor - ContentComponent(string textContent)
        /// <summary>
        /// Creates a new ContentComponent with a 'Text' object as drawn content
        /// </summary>
        /// <param name="textContent">string to use as content to draw on the screen</param>
        public ContentComponent(string textContent)
            : base()
        {
            this._contText = new Text(textContent);
        }
        #endregion

        // Creates a new ContentComponent with a 'Text' object as drawn content
        #region Constructor - ContentComponent(string textContent, SpriteFont font)
        /// <summary>
        /// Creates a new ContentComponent with a 'Text' object as drawn content
        /// </summary>
        /// <param name="textContent">the string to use as content to draw on the screen</param>s
        /// <param name="font">the SpriteFont to be used for text rendering</param>
        public ContentComponent(string textContent, SpriteFont font)
            : base()
        {
            this._contText = new Text(textContent, font);
        }
        #endregion

        // Creates a new ContentComponent with a 'Text' object as drawn content
        #region Constructor - ContentComponent(string textContent, SpriteFont font, Color textColor)
        /// <summary>
        /// Creates a new ContentComponent with a 'Text' object as drawn content
        /// </summary>
        /// <param name="textContent">the string to use as content to draw on the screen</param>s
        /// <param name="font">the SpriteFont to be used for text rendering</param>
        /// <param name="textColor">the Color to use for the text to draw</param>
        public ContentComponent(string textContent, SpriteFont font, Color textColor)
            : base()
        {
            this._contText = new Text(textContent, font, textColor);
        }
        #endregion


        #region Public Method - MaxWidth()
        public int MaxWidth()
        {
            if (IsInitialized())
            {
                int width_texture = 0;
                int width_text = 0;

                if (this._contTexture != null)
                    width_texture = this._contTexture.Width();

                if (this._contText != null)
                    width_text = this._contText.Width();

                if (width_texture > width_text)
                    return width_texture;
                else return width_text;
            }
            else return 0;
        }
        #endregion

        #region Public Method - MaxHeight()
        public int MaxHeight()
        {
            if (IsInitialized())
            {
                int height_texture = 0;
                int height_text = 0;

                if (this._contTexture != null)
                    height_texture = this._contTexture.Height();

                if (this._contText != null)
                    height_text = this._contText.Heigth();

                if (height_texture > height_text)
                    return height_texture;
                else return height_text;
            }
            else return 0;
        }
        #endregion


        // checks if the ContentComponent is initialized with one or both content types
        #region Public Method - IsInitialized()
        /// <summary>
        /// Checks the ContentComponent for successful initialized content
        /// </summary>
        /// <returns>true if successful initialized, otherwise false</returns>
        public bool IsInitialized()
        {
            if (_contText != null || _contTexture != null)
                return true;
            else return false;
        }
        #endregion

        // draws the none, one or both content types to the screen, needs an opened SpriteBatch object
        #region Public Override Method - Draw(SpriteBatch sprite)
        /// <summary>
        /// draws the content-objects to the screen using the given parameters.
        /// </summary>
        /// <param name="sprite">SpriteBatch object to use for drawing. (Begin-method needs to be invokes)</param>
        public override void Draw(SpriteBatch sprite)
        {
            try
            {
                if (Visible)
                {
                    if (_contText != null)
                        sprite.DrawString(this.ContText.Font, this.ContText.Value, this.TotalPosition, this.ContText.TextColor);
                    if (_contTexture != null)
                        sprite.Draw(this.ContTexture.Value, new Rectangle((int)(this.ContTexture.Rectangle.X + TotalPosition.X), (int)(this.ContTexture.Rectangle.Y + TotalPosition.Y), this.ContTexture.Rectangle.Width, this.ContTexture.Rectangle.Height), this.ContTexture.ModulationColor);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        #endregion
    }
}
